// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CategoryRepositoryJdbc.java

package dale.web.category.repository;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import dale.web.category.domain.Category;

// Referenced classes of package dale.web.category.repository:
//            CategoryRepository

public class CategoryRepositoryJdbc
    implements CategoryRepository
{

    public CategoryRepositoryJdbc(DataSource dataSource)
    {
        template = new NamedParameterJdbcTemplate(dataSource);
        insert = new SimpleJdbcInsert(dataSource);
        insert.withTableName("category");
        mapper = new BeanPropertyRowMapper();
    }

    public List list(Category category)
    {
        String sql = "SELECT * FROM category\nORDER BY priority\n";
        SqlParameterSource param = new BeanPropertySqlParameterSource(category);
        return template.query(sql, param, mapper);
    }

    public Category item(String categoryID)
    {
        String sql = "SELECT * FROM category\nWHERE categoryID = :categoryID\nORDER BY priority\n";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("categoryID", categoryID);
        try
        {
            return (Category)template.queryForObject(sql, param, mapper);
        }
        catch(DataAccessException e)
        {
            return null;
        }
    }

    public void insert(Category category)
    {
        SqlParameterSource param = new BeanPropertySqlParameterSource(category);
        int count = insert.execute(param);
        if(count != 1)
            throw new RuntimeException("No rows were inserted.");
        else
            return;
    }

    public void update(Category category)
    {
        String sql = "UPDATE category SET name = :name, priority = :priority, date = now()\nWHERE categoryID = :categoryID\n";
        SqlParameterSource param = new BeanPropertySqlParameterSource(category);
        int count = template.update(sql, param);
        if(count != 1)
            throw new RuntimeException("No rows were updated.");
        else
            return;
    }

    public void delete(String categoryID)
    {
        String sql = "DELETE FROM category\nWHERE categoryID = :categoryID\n";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("categoryID", categoryID);
        int count = template.update(sql, param);
        if(count != 1)
            throw new RuntimeException("No rows were deleted.");
        else
            return;
    }

    private static final String SELECT = "SELECT * FROM category\n";
    private static final String WHERE = "WHERE categoryID = :categoryID\n";
    private static final String ORDER = "ORDER BY priority\n";
    private NamedParameterJdbcTemplate template;
    private SimpleJdbcInsert insert;
    private RowMapper mapper;
}
